<?php
$fake_register_globals = false;
$sanitize_all_escapes = true;

require_once("../../globals.php");
require_once("$srcdir/patient.inc");
require_once("$srcdir/forms.inc");
require_once("$srcdir/calendar.inc");
require_once("$srcdir/formdata.inc.php");
require_once("$srcdir/options.inc.php");
require_once("$srcdir/encounter_events.inc.php");
require_once("$srcdir/acl.inc");
require_once("$srcdir/classes/postmaster.php");
require_once("$srcdir/sql.inc");

//include ('/home/saludaun/public_html/expediente/library/classes/send_appt_reminder.php');
//include ('/send_appt_reminder.php');
//The connection.txt script is executed inside the current file

$my_permission = acl_check('patients', 'appt');
// Add these restrictions back using new acl return value parameter when
//  that mechanism is added to codebase.
// if ($my_permission !== 'write' && $my_permission !== 'wsome')
if (!$my_permission)
    die(xl('Access not allowed'));

// Things that might be passed by our opener.
//
$eid = (isset($_GET['eid'])?$_GET['eid']:'');         // only for existing events
$date = (isset($_GET['date'])?$_GET['date']:'');     // this and below only for new events
$userid = $_GET['userid'];
$default_catid = (isset($_GET['catid']) ? $_GET['catid'] : '5');
//
if ($date)
    $date = substr($date, 0, 4) . '-' . substr($date, 4, 2) . '-' . substr($date, 6);
else
    $date = date("Y-m-d");
//
$starttimem = '00';
if (isset($_GET['starttimem']))
    $starttimem = substr('00' . $_GET['starttimem'], -2);
//
if (isset($_GET['starttimeh'])) {
    $starttimeh = $_GET['starttimeh'];
    if (isset($_GET['startampm'])) {
        if ($_GET['startampm'] == '2' && $starttimeh < 12)
            $starttimeh += 12;
    }
} else {
    $starttimeh = date("G");
}
$startampm = '';

$info_msg = "";
?>
<?php
    
     /*if($_SESSION['authUserID']==1){
        echo "<pre>";
        print_r($_SESSION);
        echo "</pre>";
      }*/
?>
<script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.10.0.min.js"></script>
<!--<script type="text/javascript" src="<?php echo $GLOBALS['webroot'] ?>/library/js/jquery-1.7.2.min.js"></script>-->
<?php

function InsertEventFull() {
    global $new_multiple_value, $provider, $event_date, $duration, $recurrspec, $starttime, $endtime, $locationspec;
    // =======================================
    // multi providers case
    // =======================================
    if (is_array($_POST['form_provider'])) {

        // obtain the next available unique key to group multiple providers around some event
        $q = sqlStatement("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
        $max = sqlFetchArray($q);
        $new_multiple_value = $max['max'] + 1;

        foreach ($_POST['form_provider'] as $provider) {
            $args = $_POST;
            // specify some special variables needed for the INSERT
            $args['new_multiple_value'] = $new_multiple_value;
            $args['form_provider'] = $provider;
            $args['event_date'] = $event_date;
            $args['duration'] = $duration * 60;
            $args['recurrspec'] = $recurrspec;
            $args['starttime'] = $starttime;
            $args['endtime'] = $endtime;
            $args['locationspec'] = $locationspec;
            InsertEvent($args);
        }

        // ====================================
        // single provider
        // ====================================
    } else {
        $args = $_POST;
        // specify some special variables needed for the INSERT
        $args['new_multiple_value'] = "";
        $args['event_date'] = $event_date;
        $args['duration'] = $duration * 60;
        $args['recurrspec'] = $recurrspec;
        $args['starttime'] = $starttime;
        $args['endtime'] = $endtime;
        $args['locationspec'] = $locationspec;
        InsertEvent($args);
    }
}

function validEmail($email){
    if(eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $email)) {
    return true;
    }
    return false;
}

function DOBandEncounter() {
    global $event_date, $info_msg;
    // Save new DOB if it's there.
    $patient_dob = trim($_POST['form_dob']);
    if ($patient_dob && $_POST['form_pid']) {
        $query = "UPDATE patient_data SET DOB = $patient_dob WHERE " .
                "pid = ".$_POST['form_pid'];
        sqlStatement($query);
        off_sync($query);
    }

    // Auto-create a new encounter if appropriate.
    //
	 if ($GLOBALS['auto_create_new_encounters'] && $_POST['form_apptstatus'] == '@' && $event_date == date('Y-m-d')) {
        $encounter = todaysEncounterCheck($_POST['form_pid'], $event_date, $_POST['form_comments'], $_POST['facility'], $_POST['billing_facility'], $_POST['form_provider'], $_POST['form_category'], false);
        if ($encounter) {
            $info_msg .= xl("New encounter created with id");
            $info_msg .= " $encounter";
        }
    }
}

function emailLogin($patient_id,$message){
    $patientData = sqlQuery("SELECT * FROM `patient_data` WHERE `pid`=?", array($patient_id) );
    if ( $patientData['hipaa_allowemail'] != "YES" || empty($patientData['email']) || empty($GLOBALS['patient_reminder_sender_email'])  ) {
        return false;
    }
    if (!(validEmail($patientData['email']))) {
        return false;
    }
    if (!(validEmail($GLOBALS['patient_reminder_sender_email']))) {
        return false;
    }
    
    
    $mail = new MyMailer();
    $pt_name=$patientData['fname'].' '.$patientData['lname'];
    $pt_email=$patientData['email'];
    $email_subject=xl('Access Your Patient Portal');
    $email_sender=$GLOBALS['patient_reminder_sender_email'];
    $mail->AddReplyTo($email_sender, $email_sender);
    $mail->SetFrom($email_sender, $email_sender);
    $mail->AddAddress($pt_email, $pt_name);
    $mail->Subject = $email_subject;
    $mail->MsgHTML("<html><body><div class='wrapper'>".$message."</div></body></html>");
    $mail->IsHTML(true);
    $mail->AltBody = $message;
				    
    if ($mail->Send()) {
        return true;
    } else {
        $email_status = $mail->ErrorInfo;
        error_log("EMAIL ERROR: ".$email_status,0);
        return false;
        
    }
}

function displayLogin($patient_id,$message,$emailFlag){
    $patientData = sqlQuery("SELECT * FROM `patient_data` WHERE `pid`=?", array($patient_id) );
    if ($emailFlag) {
        $message = "<br><br>" . 
                   htmlspecialchars(xl("Email was sent to following address"),ENT_NOQUOTES) . ": " .
                   htmlspecialchars($patientData['email'],ENT_NOQUOTES) . "<br><br>" .
                   $message;
    }
    //echo "<html><body onload='window.print();'>" . $message . "</body></html>";
    //echo $message;
}

//Information for email notification for new, deleted or updated appointment
function getMail($id){

    $query = "SELECT email FROM users WHERE id = $id";
    $res = sqlStatement($query);
    $email = sqlFetchArray($res);
    if(empty($mail)){
        return -1;     
    }else{
        return $email;  
    }
      
}

//Information for email notification for new, deleted or updated appointment
function getInfo($id){
    
    $query = "SELECT email, fname, lname FROM patient_data WHERE pid = $id";
    $res = sqlStatement($query);
    $info = sqlFetchArray($res);
    return $info;
    
}

//Information for email notification for new, deleted or updated appointment
function getProviderInfoAppt($id){
    if(is_array($id)){
        $nid = $id[0];
    }else if(!empty ($id)){
        $nid = $id;
    }else{
        $nid = -1;        
    }
    $query = " SELECT fname, lname "
            . " FROM users "
            . " WHERE id = $nid";
    $res = sqlStatement($query);
    $info = sqlFetchArray($res);
    return " " . $info['fname'] . " " . $info['lname']; 
}



//If there is a new patient first add it
if( $_REQUEST['patient_type'] == 'Paciente Nuevo' ){
    //echo "Please insert new patient data";
    
    $query = " INSERT INTO patient_data (fname, lname, DOB, phone_home, pubpid, t_pubpid, user_t, afil_t,empresaSeguro, seguro) ".
        " VALUES ( '".$_REQUEST['fname']."', '".$_REQUEST['lname']."' , '', '".$_REQUEST['phone']."', 
		'".$_REQUEST['doc_num']."', '".$_REQUEST['doc_type']."', 
		'".$_REQUEST['user_t']."', '".$_REQUEST['afil_t']."',
		'".$_REQUEST['form_empresaSeguro']."', '".$_REQUEST['seguro']."') ";
    //echo $query . "\n";
    $db_id = sqlInsert($query);
    
    //Insert into bitacora
    off_sync($query);
    
    $queryPid = "UPDATE patient_data SET pid = '$db_id' WHERE id='$db_id'";
    
    sqlStatement($queryPid);
       
    $pid=$db_id;
    
    //Insert into bitacora
    off_sync($queryPid);
    
    $query2 = "SELECT facility_id FROM users_facility WHERE table_id = " .$_SESSION['authUserID'];
    $res2 = sqlStatement($query2);
    $row2 = sqlFetchArray($res2);
    
    $query = " INSERT INTO patient_facility (facility_id, pid, created_date, status) VALUES ".
            " ( ".$row2['facility_id'].", ".$pid.", NOW() , 1 ) ";
    sqlStatement($query);
    //Insert into bitacora
    off_sync($query);
    
    
    $query = " INSERT INTO patient_provider (provider_id, pid, patient_general_data) VALUES ( ".$_SESSION['authUserID'].", ".$pid.", 1 ) ";
    sqlStatement($query);
    off_sync($query);
    
    
    /*echo "<pre>";
        print_r($_SESSION);
    echo "</pre>";*/
    
    $_SESSION['pid'] = $pid;
    $_POST['form_pid'] = $pid;
    $_REQUEST['form_pid'] = $pid;
    
}



//================================================================================================================
// EVENTS TO FACILITIES (lemonsoftware)
//(CHEMED) get facility name
// edit event case - if there is no association made, then insert one with the first facility
if ($eid) {
    $selfacil = '';
    $facility = sqlQuery("SELECT pc_facility, pc_multiple, pc_aid, facility.name
                            FROM openemr_postcalendar_events
                              LEFT JOIN facility ON (openemr_postcalendar_events.pc_facility = facility.id)
                              WHERE pc_eid = ?", array($eid));
    // if ( !$facility['pc_facility'] ) {
    if (is_array($facility) && !$facility['pc_facility']) {
        $qmin = sqlQuery("SELECT facility_id as minId, facility FROM users WHERE id = ?", array($facility['pc_aid']));
        $min = $qmin['minId'];
        $min_name = $qmin['facility'];

        // multiple providers case
        if ($GLOBALS['select_multi_providers']) {
            $mul = $facility['pc_multiple'];
            $query = "UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_multiple = $mul";
            sqlStatement($query);
            off_sync($query);
        }
        // EOS multiple

        $query = "UPDATE openemr_postcalendar_events SET pc_facility = $min WHERE pc_eid = $eid";
        sqlStatement($query);
        off_sync($query);
        $e2f = $min;
        $e2f_name = $min_name;
    } else {
        // not edit event
        if (!$facility['pc_facility'] && $_SESSION['pc_facility']) {
            $e2f = $_SESSION['pc_facility'];
        } elseif (!$facility['pc_facility'] && $_COOKIE['pc_facility'] && $GLOBALS['set_facility_cookie']) {
            $e2f = $_COOKIE['pc_facility'];
        } else {
            $e2f = $facility['pc_facility'];
            $e2f_name = $facility['name'];
        }
    }
}
// EOS E2F
// ===========================
//=============================================================================================================================
if ($_POST['form_action'] == "duplicate" || $_POST['form_action'] == "save") {
    // the starting date of the event, pay attention with this value
    // when editing recurring events -- JRM Oct-08
    $event_date = toDate($_POST['form_date']);
   
    // Compute start and end time strings to be saved.
    if ($_POST['form_allday']) {
        $tmph = 0;
        $tmpm = 0;
        $duration = 24 * 60;
    } else {
        $tmph = $_POST['form_hour'] + 0;
        $tmpm = $_POST['form_minute'] + 0;
        if ($_POST['form_ampm'] == '2' && $tmph < 12)
            $tmph += 12;
        $duration = $_POST['form_duration'];
    }
    $starttime = "$tmph:$tmpm:00";
    //
    $tmpm += $duration;
    while ($tmpm >= 60) {
        $tmpm -= 60;
        ++$tmph;
    }
    $endtime = "$tmph:$tmpm:00";

    // Useless garbage that we must save.
    $locationspecs = array("event_location" => "",
        "event_street1" => "",
        "event_street2" => "",
        "event_city" => "",
        "event_state" => "",
        "event_postal" => ""
    );
    $locationspec = serialize($locationspecs);

    // capture the recurring specifications
    $recurrspec = array("event_repeat_freq" => $_POST['form_repeat_freq'],
        "event_repeat_freq_type" => $_POST['form_repeat_type'],
        "event_repeat_on_num" => "1",
        "event_repeat_on_day" => "0",
        "event_repeat_on_freq" => "0",
        "exdate" => $_POST['form_repeat_exdate']
    );

    // no recurr specs, this is used for adding a new non-recurring event
    $noRecurrspec = array("event_repeat_freq" => "",
        "event_repeat_freq_type" => "",
        "event_repeat_on_num" => "1",
        "event_repeat_on_day" => "0",
        "event_repeat_on_freq" => "0",
        "exdate" => ""
    );
}//if ($_POST['form_action'] == "duplicate" || $_POST['form_action'] == "save") 
//=============================================================================================================================
if ($_POST['form_action'] == "duplicate") {

    InsertEventFull();
    DOBandEncounter();
}

// If we are saving, then save and close the window.
//
if ($_POST['form_action'] == "save") {
    /* =======================================================
     *                    UPDATE EVENTS
     * ===================================================== */
    if ($eid) {

        // what is multiple key around this $eid?
        $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));

        // ====================================
        // multiple providers
        // ====================================
        if ($GLOBALS['select_multi_providers'] && $row['pc_multiple']) {

            // obtain current list of providers regarding the multiple key
            $up = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_multiple=?", array($row['pc_multiple']));
            while ($current = sqlFetchArray($up)) {
                $providers_current[] = $current['pc_aid'];
            }

            // get the new list of providers from the submitted form
            $providers_new = $_POST['form_provider'];

            // ===== Only current event of repeating series =====
            if ($_POST['recurr_affect'] == 'current') {

                // update all existing event records to exlude the current date
                foreach ($providers_current as $provider) {
                    // update the provider's original event
                    // get the original event's repeat specs
                    $origEvent = sqlQuery("SELECT pc_recurrspec FROM openemr_postcalendar_events " .
                            " WHERE pc_aid = ? AND pc_multiple=?", array($provider, $row['pc_multiple']));
                    $oldRecurrspec = unserialize($origEvent['pc_recurrspec']);
                    $selected_date = date("Ymd", strtotime($_POST['selected_date']));
                    if ($oldRecurrspec['exdate'] != "") {
                        $oldRecurrspec['exdate'] .= "," . $selected_date;
                    } else {
                        $oldRecurrspec['exdate'] .= $selected_date;
                    }

                    // mod original event recur specs to exclude this date
                    $query = "UPDATE openemr_postcalendar_events SET " .
                            " pc_recurrspec = " . serialize($oldRecurrspec) .
                            " WHERE pc_aid = $provider AND pc_multiple=".$row['pc_multiple'];
                    sqlStatement($query);
                    off_sync($query);
                }

                // obtain the next available unique key to group multiple providers around some event
                $q = sqlStatement("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
                $max = sqlFetchArray($q);
                $new_multiple_value = $max['max'] + 1;

                // insert a new event record for each provider selected on the form
                foreach ($providers_new as $provider) {
                    // insert a new event on this date with POST form data
                    $args = $_POST;
                    // specify some special variables needed for the INSERT
                    $args['new_multiple_value'] = $new_multiple_value;
                    $args['form_provider'] = $provider;
                    $args['event_date'] = $event_date;
                    $args['duration'] = $duration * 60;
                    // this event is forced to NOT REPEAT
                    $args['form_repeat'] = "0";
                    $args['recurrspec'] = $noRecurrspec;
                    $args['form_enddate'] = "0000-00-00";
                    $args['starttime'] = $starttime;
                    $args['endtime'] = $endtime;
                    $args['locationspec'] = $locationspec;
                    InsertEvent($args);
                }
            }

            // ===== Future Recurring events of a repeating series =====
            else if ($_POST['recurr_affect'] == 'future') {
                // update all existing event records to
                // stop recurring on this date-1
                $selected_date = date("Ymd", (strtotime($_POST['selected_date']) - 24 * 60 * 60));
                foreach ($providers_current as $provider) {
                    // mod original event recur specs to end on this date
                    $query = "UPDATE openemr_postcalendar_events SET " .
                            " pc_enddate = $selected_date " .
                            " WHERE pc_aid = $provider AND pc_multiple=".$row['pc_multiple'];
                    sqlStatement($query);
                    off_sync($query);
                }

                // obtain the next available unique key to group multiple providers around some event
                $q = sqlStatement("SELECT MAX(pc_multiple) as max FROM openemr_postcalendar_events");
                $max = sqlFetchArray($q);
                $new_multiple_value = $max['max'] + 1;

                // insert a new event record for each provider selected on the form
                foreach ($providers_new as $provider) {
                    // insert a new event on this date with POST form data
                    $args = $_POST;
                    // specify some special variables needed for the INSERT
                    $args['new_multiple_value'] = $new_multiple_value;
                    $args['form_provider'] = $provider;
                    $args['event_date'] = $event_date;
                    $args['duration'] = $duration * 60;
                    $args['recurrspec'] = $recurrspec;
                    $args['starttime'] = $starttime;
                    $args['endtime'] = $endtime;
                    $args['locationspec'] = $locationspec;
                    InsertEvent($args);
                }
            } else {
                /* =================================================================== */
                // ===== a Single event or All events in a repeating series ==========
                /* =================================================================== */

                // this difference means that some providers from current was UNCHECKED
                // so we must delete this event for them
                $r1 = array_diff($providers_current, $providers_new);
                if (count($r1)) {
                    foreach ($r1 as $to_be_removed) {
                        $sql = "DELETE FROM openemr_postcalendar_events WHERE pc_aid=$to_be_removed AND pc_multiple=".$row['pc_multiple']."";
                        sqlQuery($sql);
                        //Insert into bitacora
                        off_sync($sql);
                    }
                }

                // perform a check to see if user changed event date
                // this is important when editing an existing recurring event
                // oct-08 JRM
                if (toDate($_POST['form_date']) == $_POST['selected_date']) {
                    // user has NOT changed the start date of the event
                    $event_date = fixDate($_POST['event_start_date']);
                }

                // this difference means that some providers were added
                // so we must insert this event for them
                $r2 = array_diff($providers_new, $providers_current);
                if (count($r2)) {
                    foreach ($r2 as $to_be_inserted) {
                        $args = $_POST;
                        // specify some special variables needed for the INSERT
                        $args['new_multiple_value'] = $row['pc_multiple'];
                        $args['form_provider'] = $to_be_inserted;
                        $args['event_date'] = $event_date;
                        $args['duration'] = $duration * 60;
                        $args['recurrspec'] = $recurrspec;
                        $args['starttime'] = $starttime;
                        $args['endtime'] = $endtime;
                        $args['locationspec'] = $locationspec;
                        InsertEvent($args);
                    }
                }

                // after the two diffs above, we must update for remaining providers
                // those who are intersected in $providers_current and $providers_new
                foreach ($_POST['form_provider'] as $provider) {
                    
                            $query ="UPDATE openemr_postcalendar_events SET " .
                            "pc_catid = '" . add_escape_custom($_POST['form_category']) . "', " .
                            "pc_pid = '" . add_escape_custom($_POST['form_pid']) . "', " .
                            "pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
                            "pc_time = NOW(), " .
                            "pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
                            "pc_informant = '" . add_escape_custom($_SESSION['authUserID']) . "', " .
                            "pc_eventDate = '" . add_escape_custom($event_date) . "', " .
                            "pc_endDate = '" . add_escape_custom(fixDate($_POST['form_enddate'])) . "', " .
                            "pc_duration = '" . add_escape_custom(($duration * 60)) . "', " .
                            "pc_recurrtype = '" . add_escape_custom(($_POST['form_repeat'] ? '1' : '0')) . "', " .
                            "pc_recurrspec = '" . add_escape_custom(serialize($recurrspec)) . "', " .
                            "pc_startTime = '" . add_escape_custom($starttime) . "', " .
                            "pc_endTime = '" . add_escape_custom($endtime) . "', " .
                            "pc_alldayevent = '" . add_escape_custom($_POST['form_allday']) . "', " .
                            "pc_apptstatus = '" . add_escape_custom($_POST['form_apptstatus']) . "', " .
                            "pc_prefcatid = '" . add_escape_custom($_POST['form_prefcat']) . "' ," .
                            "pc_facility = '" . add_escape_custom((int) $_POST['facility']) . "' ," . // FF stuff
                            "pc_billing_location = '" . add_escape_custom((int) $_POST['billing_facility']) . "' " .
                            "WHERE pc_aid = '" . add_escape_custom($provider) . "' AND pc_multiple = '" . add_escape_custom($row['pc_multiple']) . "'";
                            sqlStatement($query);
                            off_sync($query);
                } // foreach
            }


            // ====================================
            // single provider
            // ====================================
        } elseif (!$row['pc_multiple']) {
            if ($GLOBALS['select_multi_providers']) {
                $prov = $_POST['form_provider'][0];
            } else {
                $prov = $_POST['form_provider'];
            }

            if ($_POST['recurr_affect'] == 'current') {
                // get the original event's repeat specs
                $origEvent = sqlQuery("SELECT pc_recurrspec FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
                $oldRecurrspec = unserialize($origEvent['pc_recurrspec']);
                $selected_date = date("Ymd", strtotime($_POST['selected_date']));
                if ($oldRecurrspec['exdate'] != "") {
                    $oldRecurrspec['exdate'] .= "," . $selected_date;
                } else {
                    $oldRecurrspec['exdate'] .= $selected_date;
                }

                // mod original event recur specs to exclude this date
                
                        $query = "UPDATE openemr_postcalendar_events SET " .
                        " pc_recurrspec = ".serialize($oldRecurrspec)." " .
                        " WHERE pc_eid = $eid";
                        sqlStatement($query);
                        off_sync($query);
                        

                // insert a new event on this date with POST form data
                $args = $_POST;
                // specify some special variables needed for the INSERT
                $args['event_date'] = $event_date;
                $args['duration'] = $duration * 60;
                // this event is forced to NOT REPEAT
                $args['form_repeat'] = "0";
                $args['recurrspec'] = $noRecurrspec;
                $args['form_enddate'] = "0000-00-00";
                $args['starttime'] = $starttime;
                $args['endtime'] = $endtime;
                $args['locationspec'] = $locationspec;
                InsertEvent($args);
            } else if ($_POST['recurr_affect'] == 'future') {
                // mod original event to stop recurring on this date-1
                $selected_date = date("Ymd", (strtotime($_POST['selected_date']) - 24 * 60 * 60));
                $query  = "UPDATE openemr_postcalendar_events SET " .
                        " pc_enddate = $selected_date " .
                        " WHERE pc_eid = $eid";
                sqlStatement($query);        
                off_sync($query);
                // insert a new event starting on this date with POST form data
                $args = $_POST;
                // specify some special variables needed for the INSERT
                $args['event_date'] = $event_date;
                $args['duration'] = $duration * 60;
                $args['recurrspec'] = $recurrspec;
                $args['starttime'] = $starttime;
                $args['endtime'] = $endtime;
                $args['locationspec'] = $locationspec;
                InsertEvent($args);
            } else {

                // perform a check to see if user changed event date
                // this is important when editing an existing recurring event
                // oct-08 JRM
                if (toDate($_POST['form_date']) == $_POST['selected_date']) {
                    // user has NOT changed the start date of the event
                    $event_date = fixDate($_POST['event_start_date']);
                }

                // mod the SINGLE event or ALL EVENTS in a repeating series
                // simple provider case
                $query = "UPDATE openemr_postcalendar_events SET " .
                        "pc_catid = '" . add_escape_custom($_POST['form_category']) . "', " .
                        "pc_aid = '" . add_escape_custom($prov) . "', " .
                        "pc_pid = '" . add_escape_custom($_POST['form_pid']) . "', " .
                        "pc_title = '" . add_escape_custom($_POST['form_title']) . "', " .
                        "pc_time = NOW(), " .
                        "pc_hometext = '" . add_escape_custom($_POST['form_comments']) . "', " .
                        "pc_informant = '" . add_escape_custom($_SESSION['authUserID']) . "', " .
                        "pc_eventDate = '" . add_escape_custom($event_date) . "', " .
                        "pc_endDate = '" . add_escape_custom(fixDate($_POST['form_enddate'])) . "', " .
                        "pc_duration = '" . add_escape_custom(($duration * 60)) . "', " .
                        "pc_recurrtype = '" . add_escape_custom(($_POST['form_repeat'] ? '1' : '0')) . "', " .
                        "pc_recurrspec = '" . add_escape_custom(serialize($recurrspec)) . "', " .
                        "pc_startTime = '" . add_escape_custom($starttime) . "', " .
                        "pc_endTime = '" . add_escape_custom($endtime) . "', " .
                        "pc_alldayevent = '" . add_escape_custom($_POST['form_allday']) . "', " .
                        "pc_apptstatus = '" . add_escape_custom($_POST['form_apptstatus']) . "', " .
                        "pc_prefcatid = '" . add_escape_custom($_POST['form_prefcat']) . "' ," .
                        "pc_facility = '" . add_escape_custom((int) $_POST['facility']) . "' ," . // FF stuff
                        "pc_billing_location = '" . add_escape_custom((int) $_POST['billing_facility']) . "' " .
                        "WHERE pc_eid = '" . add_escape_custom($eid) . "'";
                sqlStatement($query);
                off_sync($query);
            }
        }

        // =======================================
        // end Update Multi providers case
        // =======================================
        // EVENTS TO FACILITIES
        $e2f = (int) $eid;
    } else {
        /* =======================================================
         *                    INSERT NEW EVENT(S)
         * ====================================================== */

        InsertEventFull();
    }

   
    // done with EVENT insert/update statements
    DOBandEncounter();
    
}

// =======================================
//    DELETE EVENT(s)
// =======================================
else if ($_POST['form_action'] == "delete") {
    // =======================================
    //  multi providers event
    // =======================================
    if ($GLOBALS['select_multi_providers']) {

        // what is multiple key around this $eid?
        $row = sqlQuery("SELECT pc_multiple FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));

        // obtain current list of providers regarding the multiple key
        $providers_current = array();
        $up = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_multiple=?", array($row['pc_multiple']));
        while ($current = sqlFetchArray($up)) {
            $providers_current[] = $current['pc_aid'];
        }

        // establish a WHERE clause
        if ($row['pc_multiple']) {
            $whereClause = "pc_multiple = '{$row['pc_multiple']}'";
        } else {
            $whereClause = "pc_eid = '$eid'";
        }

        if ($_POST['recurr_affect'] == 'current') {
            // update all existing event records to exlude the current date
            foreach ($providers_current as $provider) {
                // update the provider's original event
                // get the original event's repeat specs
                $origEvent = sqlQuery("SELECT pc_recurrspec FROM openemr_postcalendar_events " .
                        " WHERE pc_aid = ? AND pc_multiple=?", array($provider, $row['pc_multiple']));
                $oldRecurrspec = unserialize($origEvent['pc_recurrspec']);
                $selected_date = date("Ymd", strtotime($_POST['selected_date']));
                if ($oldRecurrspec['exdate'] != "") {
                    $oldRecurrspec['exdate'] .= "," . $selected_date;
                } else {
                    $oldRecurrspec['exdate'] .= $selected_date;
                }

                // mod original event recur specs to exclude this date
                $query = "UPDATE openemr_postcalendar_events SET " .
                        " pc_recurrspec = ".serialize($oldRecurrspec)." " .
                        " WHERE " . $whereClause;
                sqlStatement($query);
                //Insert into bitacora
                off_sync($query);
            }
        } else if ($_POST['recurr_affect'] == 'future') {
            // update all existing event records to stop recurring on this date-1
            $selected_date = date("Ymd", (strtotime($_POST['selected_date']) - 24 * 60 * 60));
            foreach ($providers_current as $provider) {
                // update the provider's original event
                $query = "UPDATE openemr_postcalendar_events SET " .
                        " pc_enddate = $selected_date " .
                        " WHERE " . $whereClause;
                sqlStatement($query);
                off_sync($query);
            }
        } else {
            // really delete the event from the database
            $query = "DELETE FROM openemr_postcalendar_events WHERE " . $whereClause; 
            sqlStatement($query);
            //Insert into bitacora
            off_sync($query);
        }
    }

    // =======================================
    //  single provider event
    // =======================================
    else {

        if ($_POST['recurr_affect'] == 'current') {
            // mod original event recur specs to exclude this date
            // get the original event's repeat specs
            $origEvent = sqlQuery("SELECT pc_recurrspec FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
            $oldRecurrspec = unserialize($origEvent['pc_recurrspec']);
            $selected_date = date("Ymd", strtotime($_POST['selected_date']));
            if ($oldRecurrspec['exdate'] != "") {
                $oldRecurrspec['exdate'] .= "," . $selected_date;
            } else {
                $oldRecurrspec['exdate'] .= $selected_date;
            }
            $query = "UPDATE openemr_postcalendar_events SET " .
                    " pc_recurrspec = ".serialize($oldRecurrspec)." " .
                    " WHERE pc_eid = $eid";
            sqlStatement($query);
            //Insert into bitacora
            off_sync($query);
        } else if ($_POST['recurr_affect'] == 'future') {
            // mod original event to stop recurring on this date-1
            $selected_date = date("Ymd", (strtotime($_POST['selected_date']) - 24 * 60 * 60));
            $query = "UPDATE openemr_postcalendar_events SET " .
                    " pc_enddate = $selected_date " .
                    " WHERE pc_eid = $eid";
            
            sqlStatement($query);
            //Insert into bitacora
            off_sync($query);
        } else {
            // fully delete the event from the database
                $query = "DELETE FROM openemr_postcalendar_events WHERE pc_eid = $eid";
                sqlStatement($query);
                //Insert into bitacora
                off_sync($query);
        }
    }
}

if(isset($_REQUEST['form_action']) && ($_REQUEST['form_action']=='save' || $_REQUEST['form_action']=='delete' )){
    //$handler_mail = getMail( $_SESSION['authUserID'] , 0);
	if(isset($_REQUEST['form_pid'])){
                if( $_SESSION['pid'] != ''  ){
                    $patient_info = getInfo( $_SESSION['pid'] , 1);
                }else{
                    $patient_info = getInfo( $_REQUEST['form_pid'] , 1);
                }
		
		$provider_info = getProviderInfoAppt($_REQUEST['form_provider']);
		//Create message body
		$message= ' '
		.'<p>'
		.' Estimado ' . $patient_info['fname'] . ' ' . $patient_info['lname'] . ': '
		.'</p>'
		.'<p>';   
		if($_POST['form_action']=="save"){
				$message .= 'Su cita con el(la) dr(a). ' . $provider_info . ' ha sido programada para el ' .  toDate($_POST['form_date']) . '.';    
		}else if ($_POST['form_action']=="delete"){
				$message.= 'Su cita con el(la) dr(a). '. $provider_info . ' la fecha ' .toDate($_POST['form_date']) . ' ha sido cancelada. Por favor comunicarse para proceder a la reprogramación ';
		}    
		$message.= '</p>';
		//die($_REQUEST['form_pid']." ". $message);
		emailLogin($_REQUEST['form_pid'], $message);
	}
}

if ($_POST['form_action'] != "") {
    // Close this window and refresh the calendar display.
    ?>
    <html>
        <head>
            <?php html_header_show(); ?>
            <title><?php xl('Delete Patient, Encounter, Form, Issue, Document, Payment, Billing or Transaction', 'e'); ?></title>
            <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>
            <link rel="stylesheet" href="<?php echo $web_root; ?>/patients/css/nightingale.css" type="text/css"/>
            <script language="javascript">
                setTimeout(function() {
                    parent.$.fancybox.close();
                    window.top.refreshme();
                }, 1200);
            </script>
        </head>

        <body class="body_top">
        <center>
            <p class="text">
                Guardando los cambios.
            </p>
            <p class="text">
                Este dialogo se cerrar&aacute; autom&aacute;ticamente...
            </p>
        </center>
    </body>
    </html>
    <?php
//                alert("Cita creada con exito");
//    echo "<html>\n<body>\n<script language='JavaScript'>\n";
//    if ($info_msg)
//        echo " alert('" . addslashes($info_msg) . "');\n";
////    echo " if (opener && !opener.closed && opener.refreshme) opener.refreshme();\n";
////    echo " window.close();\n";
//    echo "</script>\n</body>\n</html>\n";
    exit();
}

//*********************************
// If we get this far then we are displaying the form.
//*********************************

/* * *******************************************************************
  This has been migrate to the administration->lists
  $statuses = array(
  '-' => '',
  '*' => xl('* Reminder done'),
  '+' => xl('+ Chart pulled'),
  'x' => xl('x Cancelled'), // added Apr 2008 by JRM
  '?' => xl('? No show'),
  '@' => xl('@ Arrived'),
  '~' => xl('~ Arrived late'),
  '!' => xl('! Left w/o visit'),
  '#' => xl('# Ins/fin issue'),
  '<' => xl('< In exam room'),
  '>' => xl('> Checked out'),
  '$' => xl('$ Coding done'),
  '%' => xl('% Cancelled <  24h ')
  );
 * ******************************************************************* */

$repeats = 0; // if the event repeats
$repeattype = '0';
$repeatfreq = '0';
$patientid = '';
if ($_REQUEST['patientid'])
    $patientid = $_REQUEST['patientid'];
$origenDatos = 0;  // 1:seccion paciente exp 2: calendario
if ($_REQUEST['src'])
    $origenDatos = $_REQUEST['src'];
$patientname = '';
$patienttitle = "";
$hometext = "";
$row = array();
$informant = "";

// If we are editing an existing event, then get its data.
if ($eid) {
    // $row = sqlQuery("SELECT * FROM openemr_postcalendar_events WHERE pc_eid = $eid");

    $row = sqlQuery("SELECT e.*, u.fname, u.mname, u.lname " .
            "FROM openemr_postcalendar_events AS e " .
            "LEFT OUTER JOIN users AS u ON u.id = e.pc_informant " .
            "WHERE pc_eid = ?", array($eid));
    $informant = $row['fname'] . ' ' . $row['mname'] . ' ' . $row['lname'];

    // instead of using the event's starting date, keep what has been provided
    // via the GET array, see the top of this file
    if (empty($_GET['date']))
        $date = $row['pc_eventDate'];
    
    $date_sol = $row['date_sol'];
    
    $eventstartdate = $row['pc_eventDate']; // for repeating event stuff - JRM Oct-08
    $userid = $row['pc_aid'];
    $patientid = $row['pc_pid'];
    $starttimeh = substr($row['pc_startTime'], 0, 2) + 0;
    $starttimem = substr($row['pc_startTime'], 3, 2);
    $repeats = $row['pc_recurrtype'];
    $multiple_value = $row['pc_multiple'];

    // parse out the repeating data, if any
    $rspecs = unserialize($row['pc_recurrspec']); // extract recurring data
    $repeattype = $rspecs['event_repeat_freq_type'];
    $repeatfreq = $rspecs['event_repeat_freq'];
    $repeatexdate = $rspecs['exdate']; // repeating date exceptions

    $hometext = $row['pc_hometext'];
    if (substr($hometext, 0, 6) == ':text:')
        $hometext = substr($hometext, 6);
}
else {
    // a NEW event
    $eventstartdate = $date; // for repeating event stuff - JRM Oct-08
    //-------------------------------------
    //(CHEMED)
    //Set default facility for a new event based on the given 'userid'
    if ($userid) {
        /*         * ***********************************************************
          $pref_facility = sqlFetchArray(sqlStatement("SELECT facility_id, facility FROM users WHERE id = $userid"));
         * *********************************************************** */
        if ($_SESSION['pc_facility']) {
            $pref_facility = sqlFetchArray(sqlStatement("
		        SELECT f.id as facility_id,
		        f.name as facility
		        FROM facility f
		        WHERE f.id = ?
	          ", array($_SESSION['pc_facility'])
            ));
        } else {
            $pref_facility = sqlFetchArray(sqlStatement("
            SELECT u.facility_id, 
	          f.name as facility 
            FROM users u
            LEFT JOIN facility f on (u.facility_id = f.id)
            WHERE u.id = ?
            ", array($userid)));
        }
        /*         * ********************************************************* */
        $e2f = $pref_facility['facility_id'];
        $e2f_name = $pref_facility['facility'];
    }
    //END of CHEMED -----------------------
}

// If we have a patient ID, get the name and phone numbers to display.
if ($patientid) {
    $prow = sqlQuery("SELECT lname, fname, phone_home, phone_biz, DOB " .
            "FROM patient_data WHERE pid = ?", array($patientid));
    $patientname = $prow['lname'] . ", " . $prow['fname'];
    if ($prow['phone_home'])
        $patienttitle .= " H=" . $prow['phone_home'];
    if ($prow['phone_biz'])
        $patienttitle .= " W=" . $prow['phone_biz'];
}

// Get the providers list. //En este momento solo el que esta login TODO: Si es secretaria de un centro medico este query cambia
//$ures =//sqlStatement("SELECT id, username, fname, lname FROM users WHERE id=" . $_SESSION['authId'] .
        //" AND authorized != 0 AND active = 1 ORDER BY lname, fname");
            $query = " "
            . " select u.id, u.fname, u.lname "
            . " from users u inner join  "
            . " ( "
            . " select user_id "
            . " from user_handler "
            . " where handler_id = ".$_SESSION['authUserID']
            . " ) "
            . "  ud on (u.id = ud.user_id) "
            . "";   
    $ures = sqlStatement($query);

// Get event categories.
$cres = sqlStatement("SELECT pc_catid, pc_catname, pc_recurrtype, pc_duration, pc_end_all_day " .
        "FROM openemr_postcalendar_categories ORDER BY pc_catname");

// Fix up the time format for AM/PM.
$startampm = '1';
if ($starttimeh >= 12) { // p.m. starts at noon and not 12:01
    $startampm = '2';
    if ($starttimeh > 12)
        $starttimeh -= 12;
}
?>
<html>
    <head>
        <?php html_header_show(); ?>
        <title><?php echo $eid ? xlt('Edit') : xlt('Add New') ?> <?php echo xlt('Event'); ?></title>
        <link rel="stylesheet" href='<?php echo $css_header ?>' type='text/css'>

        <style>
            td { font-size:0.8em; }
        </style>

        <style type="text/css">@import url(../../../library/dynarch_calendar.css);</style>

        <!--<script type="text/javascript" src="../../../library/topdialog.js"></script>-->
        <script type="text/javascript" src="../../../library/dialog.js"></script>
        <script type="text/javascript" src="../../../library/textformat.js"></script>
        <script type="text/javascript" src="../../../library/dynarch_calendar.js"></script>
        <?php include_once("{$GLOBALS['srcdir']}/dynarch_calendar_en.inc.php"); ?>
        <script type="text/javascript" src="../../../library/dynarch_calendar_setup.js"></script>


        <!-- BEGIN LIBS JL -->
        <link rel="stylesheet" href="<?php echo $web_root; ?>/patients/css/nightingale.css" type="text/css"/>
        <link rel="stylesheet" href="<?php echo $web_root; ?>/patients/css/jquery.autocomplete.css" type="text/css"/>
        <link rel="stylesheet" href="<?php echo $web_root; ?>/patients/css/datepicker.css" type="text/css"/>
        <link rel="stylesheet" href="<?php echo $web_root; ?>/library/js/redmond/jquery-ui-1.10.3.custom.min.css" type="text/css"/>

        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery-1.10.0.min.js"></script>
        <!--<script type="text/javascript" src="<?php echo $web_root; ?>/library/js/common.js"></script>-->

        <link media="screen" href="<?php echo $web_root; ?>/patients/css/jquery.fancybox.css?v=2.1.5" type="text/css" rel="stylesheet">
        <script type="text/javascript" src="<?php echo $web_root; ?>/library/js/jquery.fancybox.pack.js" ></script>

        <script type="text/javascript" src="<?php echo $web_root; ?>/patients/js/jquery.ui.core.js"></script>
        <script type="text/javascript" src="<?php echo $web_root; ?>/patients/js/jquery.ui.widget.js"></script>
        <script type="text/javascript" src="<?php echo $web_root; ?>/patients/js/jquery.ui.datepicker.js"></script>
        <script type="text/javascript" src="<?php echo $web_root; ?>/patients/js/jquery.mask.min.js"></script>
        <script type="text/javascript" src="<?php echo $web_root; ?>/patients/js/jquery.autocomplete.js"></script>

        <!-- END LIBS JL -->

        <script language="JavaScript">

            var mypcc = '<?php echo $GLOBALS['phone_country_code'] ?>';

            var durations = new Array();
            // var rectypes  = new Array();
<?php
// Read the event categories, generate their options list, and get
// the default event duration from them if this is a new event.
$cattype = 0;
if ($_GET['prov'] == true) {
    $cattype = 1;
}
/*$cres = sqlStatement("SELECT pc_catid, pc_catname, pc_recurrtype, pc_duration, pc_end_all_day " .
        "FROM openemr_postcalendar_categories WHERE pc_cattype=? ORDER BY pc_catname", array($cattype));*/
$cres = sqlStatement("SELECT pc_catid, pc_catname, pc_recurrtype, pc_duration, pc_end_all_day " .
        "FROM openemr_postcalendar_categories WHERE pc_catid=9 OR pc_catid=10 ORDER BY pc_catname");
$catoptions = "";
$prefcat_options = "    <option value='0'>-- " . xlt("None") . " --</option>\n";
$thisduration = 0;
if ($eid) {
    $thisduration = $row['pc_alldayevent'] ? 1440 : round($row['pc_duration'] / 60);
}
while ($crow = sqlFetchArray($cres)) {
    $duration = round($crow['pc_duration'] / 60);
    if ($crow['pc_end_all_day'])
        $duration = 1440;
    echo " durations[" . attr($crow['pc_catid']) . "] = " . attr($duration) . "\n";
    // echo " rectypes[" . $crow['pc_catid'] . "] = " . $crow['pc_recurrtype'] . "\n";
    $catoptions .= "    <option value='" . attr($crow['pc_catid']) . "'";
    if ($eid) {
        if ($crow['pc_catid'] == $row['pc_catid'])
            $catoptions .= " selected";
    } else {
        if ($crow['pc_catid'] == $default_catid) {
            $catoptions .= " selected";
            $thisduration = $duration;
        }
    }
    $catoptions .= ">" . text(xl_appt_category($crow['pc_catname'])) . "</option>\n";

    // This section is to build the list of preferred categories:
    if ($duration) {
        $prefcat_options .= "<option value='" . attr($crow['pc_catid']) . "'";
        if ($eid) {
            if ($crow['pc_catid'] == $row['pc_prefcatid'])
                $prefcat_options .= " selected";
        }
        $prefcat_options .= ">" . text(xl_appt_category($crow['pc_catname'])) . "</option>\n";
    }
}

$cres = sqlStatement("SELECT pc_catid, pc_catname, pc_recurrtype, pc_duration, pc_end_all_day " .
        "FROM openemr_postcalendar_categories WHERE pc_catid=11 ORDER BY pc_catname");
$catoptions_prov = "";
$prefcat_options = "    <option value='0'>-- " . xlt("None") . " --</option>\n";
$thisduration = 0;
if ($eid) {
    $thisduration = $row['pc_alldayevent'] ? 1440 : round($row['pc_duration'] / 60);
}
while ($crow = sqlFetchArray($cres)) {
    $duration = round($crow['pc_duration'] / 60);
    if ($crow['pc_end_all_day'])
        $duration = 1440;
    echo " durations[" . attr($crow['pc_catid']) . "] = " . attr($duration) . "\n";
    // echo " rectypes[" . $crow['pc_catid'] . "] = " . $crow['pc_recurrtype'] . "\n";
    $catoptions_prov .= "    <option value='" . attr($crow['pc_catid']) . "'";
    if ($eid) {
        if ($crow['pc_catid'] == $row['pc_catid'])
            $catoptions_prov .= " selected";
    } else {
        if ($crow['pc_catid'] == $default_catid) {
            $catoptions_prov .= " selected";
            $thisduration = $duration;
        }
    }
    $catoptions_prov .= ">" . text(xl_appt_category($crow['pc_catname'])) . "</option>\n";

    // This section is to build the list of preferred categories:
    if ($duration) {
        $prefcat_options .= "<option value='" . attr($crow['pc_catid']) . "'";
        if ($eid) {
            if ($crow['pc_catid'] == $row['pc_prefcatid'])
                $prefcat_options .= " selected";
        }
        $prefcat_options .= ">" . text(xl_appt_category($crow['pc_catname'])) . "</option>\n";
    }
}
?>

<?php require($GLOBALS['srcdir'] . "/restoreSession.php"); ?>

            // This is for callback by the find-patient popup.
            function setpatient(pid, lname, fname, dob) {
                var f = document.forms[0];
                f.form_patient.value = lname + ', ' + fname;
                f.form_pid.value = pid;
                dobstyle = (dob == '' || dob.substr(5, 10) == '00-00') ? '' : 'none';
                document.getElementById('dob_row').style.display = dobstyle;
            }

            // This invokes the find-patient popup.
            function sel_patient() {
                dlgopen('find_patient_popup.php', '_blank', 500, 400);
            }
            
            function display_patient(value){
                if (value == 'div_patient_new'){
                    $("#div_patient_new").show();
                    $("#div_patient_reg").hide();
                }else{
                    $("#div_patient_new").hide();
                    $("#div_patient_reg").show();
                }
                
                
            }

            // Do whatever is needed when a new event category is selected.
            // For now this means changing the event title and duration.
            function set_display() {
                var f = document.forms[0];
                var s = f.form_category;
                if (s.selectedIndex >= 0) {
                    var catid = s.options[s.selectedIndex].value;
                    var style_apptstatus = document.getElementById('title_apptstatus').style;
                    var style_prefcat = document.getElementById('title_prefcat').style;
                    if (catid == '2') { // In Office
                        style_apptstatus.display = 'none';
                        style_prefcat.display = '';
                        f.form_apptstatus.style.display = 'none';
                        f.form_prefcat.style.display = '';
                    } else {
                        style_prefcat.display = 'none';
                        style_apptstatus.display = '';
                        f.form_prefcat.style.display = 'none';
                        f.form_apptstatus.style.display = '';
                    }
                }
            }

            // Do whatever is needed when a new event category is selected.
            // For now this means changing the event title and duration.
            function set_category() {
                var f = document.forms[0];
                var s = f.form_category;
                if (s.selectedIndex >= 0) {
                    var catid = s.options[s.selectedIndex].value;
                    f.form_title.value = s.options[s.selectedIndex].text;
                    f.form_duration.value = durations[catid];
                    set_display();
                }
            }

            // Modify some visual attributes when the all-day or timed-event
            // radio buttons are clicked.
            function set_allday() {
                var f = document.forms[0];
                var color1 = '#777777';
                var color2 = '#777777';
                var disabled2 = true;
                if (document.getElementById('rballday1').checked) {
                    color1 = '#000000';
                }
                if (document.getElementById('rballday2').checked) {
                    color2 = '#000000';
                    disabled2 = false;
                }
                document.getElementById('tdallday1').style.color = color1;
                document.getElementById('tdallday2').style.color = color2;
                document.getElementById('tdallday3').style.color = color2;
                document.getElementById('tdallday4').style.color = color2;
                document.getElementById('tdallday5').style.color = color2;
                f.form_hour.disabled = disabled2;
                f.form_minute.disabled = disabled2;
                f.form_ampm.disabled = disabled2;
                f.form_duration.disabled = disabled2;
            }

            // Modify some visual attributes when the Repeat checkbox is clicked.
            function set_repeat() {
                var f = document.forms[0];
                var isdisabled = true;
                var mycolor = '#777777';
                var myvisibility = 'hidden';
                if (f.form_repeat.checked) {
                    isdisabled = false;
                    mycolor = '#000000';
                    myvisibility = 'visible';
                }
                f.form_repeat_type.disabled = isdisabled;
                f.form_repeat_freq.disabled = isdisabled;
                f.form_enddate.disabled = isdisabled;
                document.getElementById('tdrepeat1').style.color = mycolor;
                document.getElementById('tdrepeat2').style.color = mycolor;
                //document.getElementById('img_enddate').style.visibility = myvisibility;
            }

            // This is for callback by the find-available popup.
            function setappt(year, mon, mday, hours, minutes) {
                var f = document.forms[0];
                f.form_date.value = '' + year + '-' +
                        ('' + (mon + 100)).substring(1) + '-' +
                        ('' + (mday + 100)).substring(1);
                f.form_ampm.selectedIndex = (hours >= 12) ? 1 : 0;
                f.form_hour.value = (hours > 12) ? hours - 12 : hours;
                f.form_minute.value = ('' + (minutes + 100)).substring(1);
            }
            
            function setappt2(mday, mon, year, hours, minutes) {
                var f = document.forms[0];
                f.form_date.value = ''+mday+'/'+mon+'/'+year+'';
                        /*
                        ('' + (mday + 100)).substring(1) + '/'
                        ('' + (mon + 100)).substring(1) + '/' +
                                '' + year;*/
                f.form_ampm.selectedIndex = (hours >= 12) ? 1 : 0;
                f.form_hour.value = (hours > 12) ? hours - 12 : hours;
                f.form_minute.value = ('' + (minutes + 100)).substring(1);
                $("#form_save").show();
            }
 
            /*function refresh(extra=''){
                var ss = document.getElementById("startdate").value;      
                var sd= document.getElementById("searchdays").value; 
                
                var catid = <?php echo $d_catid;?>;
                var facility = <?php echo $d_pc_facility;?>;
                var evdur = <?php echo $d_evdur?>;
                var eid =  <?php echo $d_eid;?>;
                var pid = <?php echo $d_providerid;?>;
                
               
                $.get('<?php echo $GLOBALS['web_root']; ?>/interface/main/calendar/find_appt_browser.php' +
                        '?providerid=' + pid +
                        '&catid=' + catid +
                        '&facility=' + facility +
                        '&startdate=' + ss +
                        '&evdur=' + evdur +
                        '&eid=' + eid +
                        '&searchdays=' + sd +
                        extra, function(data) {
                        //$("#response_find_available").html(data);
                        $("#response_find_available").html(ss + sd + pid + catid + facility + evdur);
                        //alert("Ajax terminado."+data);
                     });
            }*/

            // Invoke the find-available popup.
            function find_available(extra) {
                top.restoreSession();
                // (CHEMED) Conditional value selection, because there is no <select> element
                // when making an appointment for a specific provider
                var s = document.forms[0].form_provider;
                var f = document.forms[0].facility;
<?php if ($userid != 0) { ?>
                    s = document.forms[0].form_provider.value;
                    f = document.forms[0].facility.value;
<?php } else { ?>
                    s = document.forms[0].form_provider.options[s.selectedIndex].value;
                    f = document.forms[0].facility.options[f.selectedIndex].value;
<?php } ?>
                var c = document.forms[0].form_category;
                var formDate = document.forms[0].form_date;

                /*$.get('<?php echo $GLOBALS['web_root']; ?>/interface/main/calendar/find_appt_popup.php' +
                        '?providerid=' + s +
                        '&catid=' + c.options[c.selectedIndex].value +
                        '&facility=' + f +
                        '&startdate=' + formDate.value +
                        '&evdur=' + document.forms[0].form_duration.value +
                        '&eid=<?php echo 0 + $eid; ?>' +
                        extra, function(data) {
                    $("#.info").html(data);
                    alert("Ajax terminado."+data);
                });*/


                document.forms[0].submit();
//                parent.$.fancybox.close();
//                window.open('<?php echo $GLOBALS['web_root']; ?>/interface/main/calendar/find_appt_popup.php' +
//                        '?providerid=' + s +
//                        '&catid=' + c.options[c.selectedIndex].value +
//                        '&facility=' + f +
//                        '&startdate=' + formDate.value +
//                        '&evdur=' + document.forms[0].form_duration.value +
//                        '&eid=<?php echo 0 + $eid; ?>' +
//                        extra);
                //END (CHEMED) modifications
            }

        </script>

        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    </head>

    <body class="body_top">
		<?php

				/*
				echo "<pre>";
				print_r($_SESSION);
				echo "</pre>";
				*/
         ?>
        <form method='post' name='theform' id='theform' action='add_edit_event.php?eid=<?php echo attr($eid) ?>' />
        <!-- ViSolve : Requirement - Redirect to Create New Patient Page -->
        <input type='hidden' size='2' name='resname' value='empty' />
        <?php
        if ($_POST["resname"] == "noresult") {
            echo '
<script language="Javascript">
			// refresh and redirect the parent window
			if (!opener.closed && opener.refreshme) opener.refreshme();
			top.restoreSession();
			opener.document.location="../../new/new.php";
			// Close the window
			window.close();
</script>';
        }
        $classprov = 'current';
        $classpati = '';
        ?>
        <!-- ViSolve : Requirement - Redirect to Create New Patient Page -->
        <input type="hidden" name="form_action" id="form_action" value="">
        <input type="hidden" name="recurr_affect" id="recurr_affect" value="">
        <!-- used for recurring events -->
        <input type="hidden" name="selected_date" id="selected_date" value="<?php echo attr($date); ?>">
        <input type="hidden" name="event_start_date" id="event_start_date" value="<?php echo attr($eventstartdate); ?>">
    <center>
        <div id="recurr_popup" class="recurrent_message">
            <?php echo xlt('Apply the changes to the Current event only, to this and all Future occurrences, or to All occurrences?') ?>
            <br>
            <a type="button" class="css_button" name="all_events" id="all_events" value="  All  ">  All  </a>
            <a type="button" class="css_button" name="future_events" id="future_events" value="Future">Future</a>
            <a type="button" class="css_button" name="current_event" id="current_event" value="Current">Current</a>
            <a type="button" class="css_button" name="recurr_cancel" id="recurr_cancel" value="Cancel">Cancel</a>
        </div>
        <div class='delete_message' name="delete_response" id="delete_response">
            <span><?php echo addslashes(xl('Deleting this event cannot be undone. It cannot be recovered once it is gone. Are you sure you wish to delete this event?')); ?></span>
            <a  class="css_button" id="confirm_delete"  onclick="deleteEvent();">Si</a>
            <a  class="css_button" id="cancel_delete"  onclick="hideDeletionMsg();">No</a>
        </div>
            
        <div class="borde_normal">
            <?php if(!isset($_GET['editOn'])){ ?>
                <?php if ($origenDatos == 1) { ?>
                    <span class="historial_paciente"><i class="icon-calendar" style="font-size: 1.4em;"></i> Crear Cita </span>
                <?php } else { ?>
                    <span class="historial_paciente"><i class="icon-calendar" style="font-size: 1.4em;"></i> Crear Cita / Evento</span>
                <?php } ?>
            <?php } else { ?>
                    <span class="historial_paciente"><i class="icon-calendar" style="font-size: 1.4em;"></i> Editar Cita</span>
            <?php } ?>
            
            <!--<button class="css_button" value="Guardar" name="form_save" id='form_save' type="submit">
                <i class="icon-save"></i>
                <?php echo xla('Save'); ?>
            </button>-->
                
                <!--<?php echo "SESSION['viewtype']= ".$_SESSION['viewtype'];?> -->
                <a id="form_save" 
                   <?php
                     if(  (  ($_SESSION['viewtype'] == 'month' || !isset($_SESSION['viewtype']))
                             && !isset($_REQUEST['prov']))
                             || $_REQUEST['from_form']==1                          
                       ){
                         echo " style='display:none !important' ";
                     }
                   ?>
                   href="javascript:;" onclick=""  class="css_button" disable><span><i class="icon-ok" style="font-size: 25px;"></i> <?php xl('Save', 'e'); ?></span></a>
                
                <a href="" id="cancel" class="css_button"><span><i class="icon-reply" style="font-size: 25px;"></i> <?php xl('Cancel', 'e'); ?></span></a>
                
                <a   class="css_button" name='form_delete' id='form_delete' <?php /*echo 'value="' . xla('Delete') . '"';*/?><?php if (!$eid) echo " disabled" ?>><i style="font-size: 25px;" class="icon-remove"></i> Borrar</a>
                
                
                
                
        </div>


        <div class="borde_normal">
            <table border='0' widht="90%">
                <?php
                $provider_class = '';
                $normal = '';
                if ($_GET['prov'] == true) {
                    $provider_class = "class='current'";
                } else {
                    $normal = "class='current'";
                }
                ?>
                <!-- START TABS -->
                <?php if ($origenDatos == 2) { ?>
                    <tr><th><ul class="tabNav">
                        <?php
                        $eid = $_REQUEST["eid"];
                        $startm = $_REQUEST["startampm"];
                        $starth = $_REQUEST["starttimeh"];
                        $uid = $_REQUEST["userid"];
                        $starttm = $_REQUEST["starttimem"];
                        $dt = $_REQUEST["date"];
                        $cid = $_REQUEST["catid"];
                        ?>
                        <li <?php echo $normal; ?>>
                            <a href='add_edit_event.php?eid=<?php echo attr($eid); ?>&startampm=<?php echo attr($startm); ?>&starttimeh=<?php echo attr($starth); ?>&userid=<?php echo attr($uid); ?>&starttimem=<?php echo attr($starttm); ?>&date=<?php echo attr($dt); ?>&catid=<?php echo attr($cid); ?>&src=<?php echo $origenDatos;?>'>
                                <?php
                                echo xlt('Patient');                                
                                ?></a>
                        </li>

                        <li <?php echo $provider_class; ?>>
                            <a href='add_edit_event.php?prov=true&eid=<?php echo attr($eid); ?>&startampm=<?php echo attr($startm); ?>&starttimeh=<?php echo attr($starth); ?>&userid=<?php echo attr($uid); ?>&starttimem=<?php echo attr($starttm); ?>&date=<?php echo attr($dt); ?>&catid=<?php echo attr($cid); ?>&src=<?php echo $origenDatos;?>'>
                                <?php echo xlt('Provider'); ?></a>
                        </li>

                    </ul>
                    </th></tr>
                <?php } ?>
                <!-- END  TABS -->
                <tr><td colspan='10'>
                        <table border='0' width='100%' bgcolor='' >

                            <tr>
                                <?php if(  $_REQUEST['prov'] == false ){ ?>
                                    <td width='1%' nowrap  >
                                        <b><?php echo "Fecha Solicitud:" ?></b>
                                    </td>
                                    <td nowrap >
                                        <input name='form_date_sol' id="form_date_sol" value="<?php echo toDateView($date_sol); ?>">
                                            
                                        </input>
                                    </td>
                                    
                                    
                                    <td width='1%' nowrap  <?php if(  $_REQUEST['prov'] == false ){ ?> style="display:none" <?php } ?> >
                                    <b><?php echo ($GLOBALS['athletic_team'] ? xlt('Team/Squad') : xlt('Category')); ?>:</b>
                                </td>
                                
                                <td nowrap <?php if(  $_REQUEST['prov'] == false ){ ?> style="display:none" <?php } ?> >
                                    <select name='form_category' onchange='set_category()' style='width:100%'>
                                        <?php if( $_REQUEST['prov'] == true ){echo $catoptions_prov;}else{ echo $catoptions;} ?>
                                    </select>
                                </td>
                                    
                                <?php } else { ?>
                                
                                <td width='1%' nowrap  >
                                    <b><?php echo ($GLOBALS['athletic_team'] ? xlt('Team/Squad') : xlt('Category')); ?>:</b>
                                </td>
                                
                                <td nowrap >
                                    <select name='form_category' onchange='set_category()' style='width:100%'>
                                        <?php if( $_REQUEST['prov'] == true ){echo $catoptions_prov;}else{ echo $catoptions;} ?>
                                    </select>
                                </td>
                                <?php } ?>
                                <td width='1%' nowrap>
                                    &nbsp;&nbsp;
                                    <input type='radio' name='form_allday' onclick='set_allday()' value='1' id='rballday1'
                                           <?php if ($thisduration == 1440) echo "checked " ?>/>
                                </td>
                                <td colspan='2' nowrap id='tdallday1'>
                                    <?php echo xlt('All day event'); ?>
                                </td>
                            </tr>

                            <tr>
                                <td nowrap>
                                    <b><?php echo xlt('Date'); ?>:</b>
                                </td>
                                <td nowrap>
                                    <input type='text' <?php if( $_REQUEST['prov']==false ){echo "readonly";}?> size='10' name='form_date' id='form_date'
                                           value='<?php echo toDateView($date) ?>'
                                           title='Formato de fecha dd-mm-yyyy' />
                                    <a href="#" id="find_available" class="css_button_" onclick='Submit_Appt("",0);'
                   <?php
                     if(  $_SESSION['viewtype'] != 'month' && !isset($_REQUEST['from_form'])
                       ){
                         echo " style='display:none;' ";
                     }
                ?>
                   ><span><i  style="font-size: 25px;"></i> <?php xl('Find Available', 'e'); ?></span></a>
                                </td>
                                <td nowrap>
                                    &nbsp;&nbsp;
                                    <input type='radio' name='form_allday' onclick='set_allday()' value='0' id='rballday2' <?php if ($thisduration != 1440) echo "checked " ?>/>
                                </td>
                                <td width='1%' nowrap id='tdallday2'>
                                    <?php echo xlt('Time'); ?>
                                </td>
                                <td width='1%' nowrap id='tdallday3'>
                                    <input type='text' size='2' <?php if( $_REQUEST['prov']==false ){echo "readonly";}?> id='form_hour' name='form_hour' value='<?php echo attr($starttimeh) ?>'
                                           title='<?php echo xla('Event start time'); ?>' /> :
                                    <input type='text' size='2' <?php if( $_REQUEST['prov']==false ){echo "readonly";}?> id='form_minute' name='form_minute' value='<?php echo attr($starttimem) ?>'
                                           title='<?php echo xla('Event start time'); ?>' />&nbsp;
                                    <select name='form_ampm' id='form_ampm' title='<?php echo xla("Note: 12:00 noon is PM, not AM"); ?>' >
                                        <option value='1'><?php echo xlt('AM'); ?></option>
                                        <option value='2'<?php if ($startampm == '2') echo " selected" ?>><?php echo xlt('PM'); ?></option>
                                    </select>
                                </td>
                            </tr>

                            <tr>
                                <td nowrap>
                                    <b><?php echo ($GLOBALS['athletic_team'] ? xlt('Team/Squad') : xlt('Title')); ?>:</b>
                                </td>
                                <td nowrap>
                                    <input type='text' size='10' name='form_title' value='<?php echo attr($row['pc_title']); ?>'
                                           style='width:100%'
                                           title='<?php echo xla('Event title'); ?>' />
                                </td>
                                <td nowrap>&nbsp;

                                </td>
                                <td nowrap id='tdallday4'><?php echo xlt('duration'); ?>
                                </td>
                                <td nowrap id='tdallday5'>
                                    <input type='text' size='4' name='form_duration' value='<?php echo attr($thisduration) ?>' title='<?php echo xla('Event duration in minutes'); ?>' />
                                    <?php echo xlt('minutes'); ?>
                                </td>
                            </tr>

                            <tr>
                                <td nowrap><b><?php echo xlt('Facility'); ?>:</b></td>
                                <td>

                                    <?php
                                    // echo $_SESSION['authorizedUser']."---";

                                    /* $facils = getUserFacilities($_SESSION['authId']);

                                      echo "<pre>";
                                      print_r($facils);
                                      echo "</pre>"; */
                                    ?>
                                    <select name="facility" id="facility" >
                                        <?php
                                        $facils = getUserFacilities($_SESSION['authId']); // establecimientos ligados al medico
                                        //$e2f = getDefaultFacilityIdFromUser($_SESSION['authId']);//establecimiento por defecto
                                        //$qsql = sqlStatement("SELECT id, name FROM facility WHERE service_location != 0");

                                        foreach ($facils as $facil) {
                                            $selected = ( $facil['id'] == $e2f ) ? 'selected="selected"' : '';
                                            echo "<option value='" . attr($facil['id']) . "' $selected>" . text($facil['name']) . "</option>";
                                        }
                                        ?>
                                        <?php //END (CHEMED) IF  ?>
                                </td>
                                </select>
                            </tr>

<!--<tr>
    <td nowrap>
        <b><?php echo xlt('Billing Facility'); ?>:</b>
    </td>
    
    <td>
                            <?php
                            billing_facility('billing_facility', $row['pc_billing_location']);
                            ?>
    </td>
</tr>-->
                            <?php
                            if ($_GET['prov'] != true) {
                                ?>
                                <tr id="patient_details">
                                    <td nowrap>
                                        <b><?php echo xlt('Patient'); ?>:</b>
                                    </td>
                                    <td nowrap>
                                        
                                        <input type="radio" id="reg_patient" name="patient_type" value="Paciente Regular" onclick="display_patient('div_patient_reg');"></input>Paciente Regular
                                        <input type="radio" id="new_patient" name="patient_type" value="Paciente Nuevo" onclick="display_patient('div_patient_new');"></input>Paciente Nuevo<br>
                                        <div id="div_patient_reg" style="display:none">
                                            <input type='text' size='10' name='form_patient' id="form_patient" style='width:100%;' value='<?php echo attr($patientname); ?>'  />
                                        </div>
                                        
                                        
                                         
                                        
                                        <div id="div_patient_new" style="display:none">
                                            <div id="resumenCita">
												<ul><li class='label'>Primer Nombre</li><li><input type="text" name="fname"></input></li></ul>
												<ul><li class='label'>Primer Apellido</li><li><input type="text" name="lname"></input></li></ul>
												<ul>
													<li class='label'>Tipo de documento</li>
													<li>
														<select name="doc_type" sytle="width:50px;">
															<option value='_blank'><?php xl('Select One', 'e'); ?></option>
															<?php
															
																$cres = sqlStatement("SELECT * FROM `list_options` WHERE list_id = 'Tipo_de_Identificacion' order by seq asc");
																while ($crow = sqlFetchArray($cres)) {
																	echo "       <option value='".$crow['option_id']."'";
																	if( $crow['seq'] == $result['pc_causaid'] ) echo " selected ";
																	echo ">" . $crow['title'] . "</option>\n";
																}
															
															?>
														</select>
													</li>
												</ul>
												<ul><li class='label'>Numero de documento</li><li><input type="text" maxlength="16" name="doc_num"></input></li></ul>
												<ul><li class='label'>Telefono</li><li><input type="text" name="phone" maxlength="10"></input></li></ul>
												<ul><li class='label'>Tipo de usuario</li>
													<li>
														<select name="user_t" id="user_t" sytle="width:50px;">
															<option value='_blank'><?php xl('Select One', 'e'); ?></option>
															<?php
															
																$cres = sqlStatement("SELECT * FROM `list_options` WHERE list_id = 'tipodeusuario' order by seq asc");
																while ($crow = sqlFetchArray($cres)) {
																	echo "       <option value='".$crow['seq']."'";
																	if( $crow['seq'] == $result['pc_causaid'] ) echo " selected ";
																	echo ">" . $crow['title'] . "</option>\n";
																}
															
															?>
												</select></li></ul>
												<ul><li class='label'>Tipo de afiliación</li><li><select name="afil_t" id="afil_t" sytle="width:50px;">
														<?php
															
																$cres = sqlStatement("SELECT * FROM `list_options` WHERE list_id = 'tipodeafiliacion' order by seq asc");
																while ($crow = sqlFetchArray($cres)) {
																	echo "       <option value='".$crow['seq']."'";
																	if( $crow['seq'] == $result['pc_causaid'] ) echo " selected ";
																	echo ">" . $crow['title'] . "</option>\n";
																}
															
														?>
												</select></li>
												</ul>
												<ul>
													<li class='label'>Empresas de Afiliación:</li>
													<li>
														<textarea rows="2" cols="35" title="Empresas de Afiliación" id="form_empresaSeguro" name="form_empresaSeguro" style="background-color: rgb(251, 251, 251);"></textarea>
													</li>
												</ul>
											</div>
												
                                        </div>
                                        <input type='hidden' name='form_pid' id="form_pid" value='<?php echo attr($patientid) ?>' />
                                        
                                        
                                    </td>
                                    <td colspan='3' nowrap style='font-size:8pt'>
                                        &nbsp;
                                        <span class="infobox">
                                            <?php
                                            if ($patienttitle != "") {
                                                //echo $patienttitle;
                                            }
                                            ?>
                                        </span>
                                    </td>
                                </tr>
                                <?php
                            }
                            ?>
                            <tr>
                                <td nowrap>
                                    <b><?php echo xlt('Provider'); ?>:</b>
                                </td>
                                <td nowrap>

                                    <?php
// =======================================
// multi providers 
// =======================================
                                    if ($GLOBALS['select_multi_providers']) {

                                        //  there are two posible situations: edit and new record
                                        // this is executed only on edit ($eid)
                                        if ($eid) {
                                            if ($multiple_value) {
                                                // find all the providers around multiple key
                                                $qall = sqlStatement("SELECT pc_aid AS providers FROM openemr_postcalendar_events WHERE pc_multiple = ?", array($multiple_value));
                                                while ($r = sqlFetchArray($qall)) {
                                                    $providers_array[] = $r['providers'];
                                                }
                                            } else {
                                                $qall = sqlStatement("SELECT pc_aid AS providers FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
                                                $providers_array = sqlFetchArray($qall);
                                            }
                                        }
										
										/*print_r(sqlFetchArray($ures)); 
										echo $_SESSION['authUserID'];*/
										
										/*print_r(sqlFetchArray($ures)); 
										echo "sesion----".$_SESSION['pc_username'];*/
										
                                        // build the selection tool
                                        echo "<select id='form_provider' name='form_provider[]' style='width:100%' multiple='multiple' size='5' >";
										/*echo"<pre>"	;
										print_r($_SESSION);
										echo"</pre>"	;*/
										
                                        while ($urow = sqlFetchArray($ures)) {
                                            echo "    <option value='" . attr($urow['id']) . "'";

                                            //if ($userid) {
												//$test=1;
                                                if(is_array($_SESSION['pc_username'])){
                                                    if( $urow['id'] == $_SESSION['pc_username'][0] || $_SESSION['pc_username'][0]=="__PC_ALL__")
                                                        echo " selected ";
														//$test=0;
                                                }else {
													
                                                    if( $urow['id'] == $_SESSION['pc_username'] || $_SESSION['pc_username']=="__PC_ALL__")
                                                        echo " selected ";
														//$test=0;
                                                }
                                                if((mysqli_num_rows($ures)==1 || mysqli_num_rows($ures)==0 ) /*&& $test==1*/){
													echo " selected ";
												}
                                            //}

                                            echo ">" . text($urow['lname']);
                                            if ($urow['fname'])
                                                echo ", " . text($urow['fname']);
                                            echo "</option>\n";
                                        }

                                        echo '</select>';

// =======================================
// single provider 
// =======================================
                                    } else {

                                        if ($eid) {
                                            // get provider from existing event
                                            $qprov = sqlStatement("SELECT pc_aid FROM openemr_postcalendar_events WHERE pc_eid = ?", array($eid));
                                            $provider = sqlFetchArray($qprov);
                                            $defaultProvider = $provider['pc_aid'];
                                        } else {
                                            // this is a new event so smartly choose a default provider 
                                            /*                                             * ***************************************************************
                                              if ($userid) {
                                              // Provider already given to us as a GET parameter.
                                              $defaultProvider = $userid;
                                              }
                                              else {
                                              // default to the currently logged-in user
                                              $defaultProvider = $_SESSION['authUserID'];
                                              // or, if we have chosen a provider in the calendar, default to them
                                              // choose the first one if multiple have been selected
                                              if (count($_SESSION['pc_username']) >= 1) {
                                              // get the numeric ID of the first provider in the array
                                              $pc_username = $_SESSION['pc_username'];
                                              $firstProvider = sqlFetchArray(sqlStatement("select id from users where username='".$pc_username[0]."'"));
                                              $defaultProvider = $firstProvider['id'];
                                              }
                                              }
                                              }

                                              echo "<select name='form_provider' style='width:100%' />";
                                              while ($urow = sqlFetchArray($ures)) {
                                              echo "    <option value='" . $urow['id'] . "'";
                                              if ($urow['id'] == $defaultProvider) echo " selected";
                                              echo ">" . $urow['lname'];
                                              if ($urow['fname']) echo ", " . $urow['fname'];
                                              echo "</option>\n";
                                              }
                                              echo "</select>";
                                             * *************************************************************** */
                                            // default to the currently logged-in user
                                            $defaultProvider = $_SESSION['authUserID'];
                                            // or, if we have chosen a provider in the calendar, default to them
                                            // choose the first one if multiple have been selected
                                            /* if (count($_SESSION['pc_username']) >= 1) {
                                              // get the numeric ID of the first provider in the array
                                              $pc_username = $_SESSION['pc_username'];
                                              $firstProvider = sqlFetchArray(sqlStatement("select id from users where username=?", array($pc_username[0])));
                                              $defaultProvider = $firstProvider['id'];
                                              } */
                                            // if we clicked on a provider's schedule to add the event, use THAT.
                                            if ($userid)
                                                $defaultProvider = $userid;
                                        }
										/*print_r(sqlFetchArray($ures)); 
										echo "sesion----".$_SESSION['pc_username'];*/
                                        echo "<select name='form_provider' id='form_provider' style='width:100%' />";
                                        while ($urow = sqlFetchArray($ures)) {
                                            echo "    <option value='" . attr($urow['id']) . "'";
                                            if(is_array($_SESSION['pc_username'])){
                                                if( $urow['id'] == $_SESSION['pc_username'][0] )
                                                    echo " selected";
                                            }else{
                                                if( $urow['id'] == $_SESSION['pc_username'] )
                                                    echo "selected";
                                            }
                                            echo ">" . text($urow['lname']);
                                            if ($urow['fname'])
                                                echo ", " . text($urow['fname']);
                                            echo "</option>\n";
                                        }
                                        echo "</select>";
                                        /*                                      * ************************************************************* */
                                    }
                                    ?>

                                </td>
                                <td nowrap>
                                    &nbsp;&nbsp;
                                    <input type='checkbox' name='form_repeat' onclick='set_repeat(this)' value='1'<?php if ($repeats) echo " checked" ?>/>
                                    <input type='hidden' name='form_repeat_exdate' id='form_repeat_exdate' value='<?php echo attr($repeatexdate); ?>' /> <!-- dates excluded from the repeat -->
                                </td>
                                <td nowrap id='tdrepeat1'><?php echo xlt('Repeats'); ?>
                                </td>
                                <td nowrap>

                                    <select name='form_repeat_freq' title='<?php echo xla('Every, every other, every 3rd, etc.'); ?>'>
                                        <?php
                                        foreach (array(1 => xl('every'), 2 => xl('2nd'), 3 => xl('3rd'), 4 => xl('4th'), 5 => xl('5th'), 6 => xl('6th'))
                                        as $key => $value) {
                                            echo "    <option value='" . attr($key) . "'";
                                            if ($key == $repeatfreq)
                                                echo " selected";
                                            echo ">" . text($value) . "</option>\n";
                                        }
                                        ?>
                                    </select>

                                    <select name='form_repeat_type'>
                                        <?php
// See common.api.php for these:
                                        foreach (array(0 => xl('day'), 4 => xl('workday'), 1 => xl('week'), 2 => xl('month'), 3 => xl('year'))
                                        as $key => $value) {
                                            echo "    <option value='" . attr($key) . "'";
                                            if ($key == $repeattype)
                                                echo " selected";
                                            echo ">" . text($value) . "</option>\n";
                                        }
                                        ?>
                                    </select>

                                </td>
                            </tr>

                            <tr>
                                <td nowrap>
                                    <span id='title_apptstatus'><b><?php echo ($GLOBALS['athletic_team'] ? xlt('Session Type') : xlt('Status')); ?>:</b></span>
                                    <span id='title_prefcat' style='display:none'><b><?php echo xlt('Pref Cat'); ?>:</b></span>
                                </td>
                                <td nowrap>

                                    <?php
                                    generate_form_field(array('data_type' => 1, 'field_id' => 'apptstatus', 'list_id' => 'apptstat', 'empty_title' => 'SKIP'), $row['pc_apptstatus']);
                                    ?>
                                    <!--
                                     The following list will be invisible unless this is an In Office
                                     event, in which case form_apptstatus (above) is to be invisible.
                                    -->
                                    <select name='form_prefcat' style='width:100%;display:none' title='<?php echo xla('Preferred Event Category'); ?>'>
                                        <?php echo $prefcat_options ?>
                                    </select>

                                </td>
                                <td nowrap>&nbsp;

                                </td>
                                <td nowrap id='tdrepeat2'><?php echo xlt('until'); ?>
                                </td>
                                <td nowrap>
                                    
                                    <input type='text' size='10' name='form_enddate' id='form_enddate'
                                           value='<?php echo toDateView($date)?>'
                                           title='Formato de fecha dd-mm-yyyy' />
                                         <?php
                                         if ($repeatexdate != "") {
                                             $tmptitle = "The following dates are excluded from the repeating series";
                                             if ($multiple_value) {
                                                 $tmptitle .= " for one or more providers:\n";
                                             } else {
                                                 $tmptitle .= "\n";
                                             }
                                             $exdates = explode(",", $repeatexdate);
                                             foreach ($exdates as $exdate) {
                                                 $tmptitle .= date("d M Y", strtotime($exdate)) . "\n";
                                             }
                                             echo "<a href='#' title='" . attr($tmptitle) . "' alt='" . attr($tmptitle) . "'><img src='../../pic/warning.gif' title='" . attr($tmptitle) . "' alt='*!*' style='border:none;'/></a>";
                                         }
                                         ?>
                                </td>
                            </tr>

                            <tr>
                                <td nowrap>
                                    <b><?php echo xlt('Comments'); ?>:</b>
                                </td>
                                <td colspan='1' nowrap>
                                    <input type='text'  name='form_comments' style='width:100%;' value='<?php echo attr($hometext); ?>' title='<?php echo xla('Optional information about this event'); ?>' />
                                </td>
                            </tr>

                            <?php
// DOB is important for the clinic, so if it's missing give them a chance
// to enter it right here.  We must display or hide this row dynamically
// in case the patient-select popup is used.
                            $patient_dob = trim($prow['DOB']);
                            $dobstyle = ($prow && (!$patient_dob || substr($patient_dob, 5) == '00-00')) ?
                                    '' : 'none';
                            ?>
                            <tr id='dob_row' style='display:<?php echo $dobstyle ?>'>
                                <td colspan='4' nowrap>
                                    <b><font color='red'><?php echo xlt('DOB is missing, please enter if possible'); ?>:</font></b>
                                </td>
                                <td nowrap>
                                    <input type='text' size='10' name='form_dob' id='form_dob' title='<?php echo xla('yyyy-mm-dd date of birth'); ?>' onkeyup='datekeyup(this, mypcc)' onblur='dateblur(this, mypcc)' />
                                    <img src='../../pic/show_calendar.gif' align='absbottom' width='24' height='22'
                                         id='img_dob' border='0' alt='[?]' style='cursor:pointer;cursor:hand'
                                         title='<?php echo xla('Click here to choose a date'); ?>'>
                                </td>
                            </tr>

                        </table></td></tr>
                <tr class='text'>
                    <td colspan='10'>

<!--                        <input type='button' name='form_save' id='form_save' value='<?php echo xla('Save'); ?>' />

                        <?php if (!($GLOBALS['select_multi_providers'])) { //multi providers appt is not supported by check slot avail window, so skip     ?>
                                                                                    <input type='button' id='find_available' value='<?php echo xla('Find Available'); ?>' />
                        <?php } ?>
                        &nbsp;-->
                       <!--input type='button' name='form_delete' id='form_delete' value='<?php echo xla('Delete'); ?>'<?php if (!$eid) echo " disabled" ?> /-->
<!--                        <input type='button' id='cancel' value='<?php echo xla('Cancel'); ?>' />-->
                        <!--                        &nbsp;
                                                <input type='button' name='form_duplicate' id='form_duplicate' value='<?php echo xla('Create Duplicate'); ?>' />-->
                    </td>
                </tr>
            </table>
        </div>
        <?php if ($informant) echo "<p class='text'>" . xlt('Last update by') . " $informant " . xlt('on') . " " . text($row['pc_time']) . "</p>\n"; ?>
    </center>
	<input id="seguro" type="hidden" name="seguro">
</form>
        <div class="response_find_available" id="response_find_available">
        </div>



</body>

<script language='JavaScript'>
<?php if ($eid) { ?>
                set_display();
<?php } else { ?>
                set_category();
<?php } ?>
            set_allday();
            set_repeat();
            /*$("#form_date").datepicker({
                showOn: "button",
                buttonImage: "<?php echo $web_root; ?>/patients/images/calendar-green.gif",
                dateFormat: 'dd/mm/yy',
                changeMonth: true,
                changeYear: true,
                buttonImageOnly: true
            });*/
    
            $("#form_date_sol").datepicker({
                showOn: "button",
                buttonImage: "<?php echo $web_root; ?>/patients/images/calendar-green.gif",
                dateFormat: 'dd/mm/yy',
                changeMonth: true,
                changeYear: true,
                buttonImageOnly: true
            });
            $("#form_date_sol").mask("99/99/9999");
            
            $("#form_enddate").datepicker({
                showOn: "button",
                buttonImage: "<?php echo $web_root; ?>/patients/images/calendar-green.gif",
                dateFormat: 'dd/mm/yy',
                changeMonth: true,
                changeYear: true,
                buttonImageOnly: true
            });

            

            $("#form_date").mask("99/99/9999");
//            Calendar.setup({inputField: "form_date", ifFormat: "%Y-%m-%d", button: "img_date"});
            //Calendar.setup({inputField: "form_enddate", ifFormat: "%Y-%m-%d", button: "img_enddate"});
            //Calendar.setup({inputField: "form_dob", ifFormat: "%Y-%m-%d", button: "img_dob"});      
            
</script>

<script language="javascript">
// jQuery stuff to make the page a little easier to use

    $(document).ready(function() {

        $("#form_patient").autocomplete("<?php echo $web_root; ?>/interface/patients_autocomplete.php",
                {
                    minChars: 3,
                    matchSubset: 1,
                    matchContains: 1,
                    selectFirst: false,
                    cacheLength: 10,
                    autoFill: false,
                    onItemSelect: function(li) {
                        $("#form_patient").val($("<div>").html(li.selectValue).text());
                    }
                });

        $("#form_patient").result(function(event, data, formatted) {
//            $("#form_patient").val(data[0]);
            $("#form_pid").val(data[1]);
        });
		
		$("#form_empresaSeguro").autocomplete("<?php echo $web_root; ?>/interface/seguros_autocomplete.php",
                        {
                            minChars: 3,
                            matchSubset: 1,
                            matchContains: 1,
                            selectFirst: false,
                            cacheLength: 10,
                            autoFill: false,
                            onItemSelect: function(li) {
                                $("#form_empresaSeguro").val($("<div>").html(li.selectValue).text());
                            }
                        });

						$("#form_empresaSeguro").result(function(event, data, formatted) {
				//            $("#form_patient").val(data[0]);
							$("#seguro").val(data[1]);
						});
		

        $("#form_save").click(function() {
            validate("save");
        });
        $("#form_duplicate").click(function() {
            validate("duplicate");
        });
        $("#find_available").click(function() {
            //
        });
        $("#form_delete").click(function() {
            showDeletionMsg();
        });
        $("#cancel").click(function() {
            parent.jQuery.fancybox.close();
        });

        // buttons affecting the modification of a repeating event
        $("#all_events").click(function() {
            $("#recurr_affect").val("all");
            EnableForm();
            SubmitForm();
        });
        $("#future_events").click(function() {
            $("#recurr_affect").val("future");
            EnableForm();
            SubmitForm();
        });
        $("#current_event").click(function() {
            $("#recurr_affect").val("current");
            EnableForm();
            SubmitForm();
        });
        $("#recurr_cancel").click(function() {
            $("#recurr_affect").val("");
            EnableForm();
            HideRecurrPopup();
        });
    });
    
    

// Check for errors when the form is submitted.
    function validate(valu) {
        var f = document.getElementById('theform');
        
        
        var sst_date = f.form_date.value.split('/');
        var eed_date = f.form_enddate.value.split('/');
        
        var st_date = new Date(sst_date[2],sst_date[1],sst_date[0]);
        var ed_date = new Date(eed_date[2],eed_date[1],eed_date[0]);

        
        if (f.form_repeat.checked &&
                (!f.form_enddate.value || st_date.getTime() > ed_date.getTime())) {
            alert('<?php echo addslashes(xl("An end date later than the start date is required for repeated events!")); ?>');
            return false;
        }
<?php
if ($_GET['prov'] != true) {
    ?>
        if(!$("#new_patient").is(':checked')){
            if (f.form_pid.value == '') {
                alert('<?php echo addslashes(xl('Patient Name Required')); ?>');
                return false;
            }
        }else{
            if (f.fname.value == '' || f.lname.value == '' ||  f.doc_type.value == '_blank'
				|| f.doc_num.value == '' ||  f.phone.value == ''
				|| f.user_t.value=='_blank'	|| f.afil_t.value=='_blank'
				|| f.form_empresaSeguro== ''
				){
                //alert('<?php echo addslashes(xl('Patient Name Required')); ?>');
                alert('<?php echo addslashes("Si el paciente es nuevo, todos los datos son obligatorios."); ?>');
                return false;
            }
        
        }
    <?php
}
?>
        $('#form_action').val(valu);

<?php if ($repeats): ?>
            // existing repeating events need additional prompt
            if ($("#recurr_affect").val() == "") {
                DisableForm();
                // show the current/future/all DIV for the user to choose one
                $("#recurr_popup").css("display", "block");
                
                return false;
            }
<?php endif; ?>

        return SubmitForm();
    }

// disable all the form elements outside the recurr_popup
    function DisableForm() {
        $("#theform").children().attr("disabled", "true");
    }
    function EnableForm() {
        $("#theform").children().removeAttr("disabled");
    }
// hide the recurring popup DIV
    function HideRecurrPopup() {
        $("#recurr_popup").css("visibility", "hidden");
    }

    
    function showDeletionMsg(){
        $("#delete_response").css("display", "block");
        
    }
    
    function hideDeletionMsg(){
        $("#delete_response").css("display", "none");
    }
    
    function showRecurrentMsg(){
        $("#recurr_popup").css("display","block");
    }

    function hideRecurrentMsg(){
        $("#recurr_popup").css("display","none");
    }

    function deleteEvent() {
        
        //if (confirm("<?php echo addslashes(xl('Deleting this event cannot be undone. It cannot be recovered once it is gone. Are you sure you wish to delete this event?')); ?>")) {
            $('#form_action').val("delete");

<?php if ($repeats): ?>
                // existing repeating events need additional prompt
                if ($("#recurr_affect").val() == "") {
                    DisableForm();
                    // show the current/future/all DIV for the user to choose one
                    $("#recurr_popup").css("display", "block");
                    return false;
                }
<?php endif; ?>

            return SubmitForm();
        //}
        
    }

    function SubmitForm() {
        var f = document.forms[0];
        <?php if (!($GLOBALS['select_multi_providers'])) { // multi providers appt is not supported by check slot avail window, so skip                    ?>
            if (f.form_action.value != 'delete') {
                // Check slot availability.
                var mins = parseInt(f.form_hour.value) * 60 + parseInt(f.form_minute.value);
                if (f.form_ampm.value == '2' && mins < 720)
                    mins += 720;
                find_available('&cktime=' + mins);
            }
            else {
                top.restoreSession();
                f.submit();
            }
        <?php } else { ?>
            top.restoreSession();
            f.submit();
        <?php } ?>

        return true;
    }
    
  


    function Submit_Appt(extra , type){
        top.restoreSession();

        var s = document.forms[0].form_provider;
        var f = document.forms[0].facility;
<?php if ($userid != 0) { ?>
            s = document.forms[0].form_provider.value;
            f = document.forms[0].facility.value;
<?php } else { ?>
            s = document.forms[0].form_provider.options[s.selectedIndex].value;
            f = document.forms[0].facility.options[f.selectedIndex].value;
<?php } ?>
        var c = document.forms[0].form_category;
        var formDate = document.forms[0].form_date;
        
        
        var message = "";
        if (type==1){
            var ss = document.getElementById("startdate").value;      
            var sd= document.getElementById("searchdays").value;     
            var n= ss.split("/");
            ss = n[2] + "/" + n[1] + "/" + n[0];
            
            message = "&startdate=" + ss +
                          "&searchdays=" + sd;
            extra += message;
        }else{
            message = "&startdate=" + formDate.value;
            extra += message;
        }
                // (CHEMED) Conditional value selection, because there is no <select> element
                // when making an appointment for a specific provider

        $.get('<?php echo $GLOBALS['web_root']; ?>/interface/main/calendar/find_appt_browser.php' +
            '?providerid=' + s +
            '&catid=' + c.options[c.selectedIndex].value +
            '&facility=' + f +
            '&evdur=' + document.forms[0].form_duration.value +
            '&eid=<?php echo 0 + $eid; ?>' +
            extra, function(data) {
            $("#response_find_available").html(data);
         });
         

    }
    
        function Submit_Srch(extra, type){
                       top.restoreSession();
                // (CHEMED) Conditional value selection, because there is no <select> element
                // when making an appointment for a specific provider
                
                var message = "";
                if (type==1){
                    var ss = document.getElementById("startdate").value;      
                    var sd= document.getElementById("searchdays").value;     
                    
                    message = "&startdate=" + ss +
                                  "&searchdays=" + sd;
                    extra += message;
                }else{
                    message = "&startdate=" + formDate.value;
                    extra += message;
                }
                    
                
                
                
                var s = document.forms[0].form_provider;
                var f = document.forms[0].facility;
                
<?php if ($userid != 0) { ?>
                    s = document.forms[0].form_provider.value;
                    f = document.forms[0].facility.value;
<?php } else { ?>
                    s = document.forms[0].form_provider.options[s.selectedIndex].value;
                    f = document.forms[0].facility.options[f.selectedIndex].value;
<?php } ?>
                var c = document.forms[0].form_category;
                var formDate = document.forms[0].form_date;
        $.get('<?php echo $GLOBALS['web_root']; ?>/interface/main/calendar/find_appt_browser.php' +
            '?providerid=' + s +
            '&catid=' + c.options[c.selectedIndex].value +
            '&facility=' + f +
            '&evdur=' + document.forms[0].form_duration.value +
            '&eid=<?php echo 0 + $eid; ?>' +
            extra, function(data) {
            $("#response_find_available").html(data);
            //alert("Ajax terminado."+data);
         });
    }

</script>

</html>
